<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Comment;
use app\admin\model\Address;
use app\common\model\Category as CategoryModel;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $LitestoreGoods = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
        $this->LitestoreGoods = new \app\admin\model\LitestoreGoods;
    }  

    /**
     * 门店列表 
     */
    public function mendian_list(){
      $data = Db::name('mendian')->order('weigh desc')->select();
      $this->success('门店列表',$data);
    }
    /**
     * 获取省市区 
     */
    public function shengshiqu(){
        $shengshiqu = Db::name('area')->field("id,pid,name,1 as level")->where('pid',0)->select();
        
        foreach ($shengshiqu as $k => $v) {
            $city = Db::name('area')->field("id,pid,name,2 as level")->where('pid',$v['id'])->select();
            // $shengshiqu[$k]['city'] = $city;
            foreach ($city as $kk => $vv) {
                $city[$kk]['area']=Db::name('area')->field("id,pid,name,3 as level")->where('pid',$vv['id'])->select();
            }
            $shengshiqu[$k]['city'] = $city;
            
        }
 

        $this->success('获取省市区',$shengshiqu);
    }
    
    /**
     * 我的收货地址列表 isdefault等于1为默认地址
     */
    public function address_list(){
      $data = Db::name('address')->where(['user_id'=>$this->user_id])->order('isdefault desc,updatetime desc')->select();
      $this->success('收货地址列表',$data);
    }

    /**
     * 收货地址详情
     * @param int $address_id
     */
    public function address_detail(){
      $data = Db::name('address')->where(['id'=>input('address_id')])->find();
      $this->success('收货地址详情',$data);
    }
    /**
     * 收货地址删除
     * @param int $address_id
     */
    public function address_delete(){
      $now_data = Db::name('address')->where(['id'=>input('address_id')])->find();
      if($now_data['isdefault']==1){
        $this->error('默认收货地址无法删除');
      }
      $data = Db::name('address')->where(['id'=>input('address_id')])->delete();
      if($data)$this->success('操作成功');
      $this->error('操作失败');
    }

     /**
     * 收货地址添加,修改以及设置默认收货地址。修改和设置默认情况传 address_id 过来。添加时候不用传address_id。
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/action_address)
     * @ApiParams   (name="phone",type=int,required=false,description="手机号码")
     * @ApiParams   (name="name",type=string,required=false,description="收货人姓名")
     * @ApiParams   (name="detail",type=string,required=false,description="详细地址")
     * @ApiParams   (name="province",type=int,required=false,description="省")
     * @ApiParams   (name="city",type=string,required=false,description="市")
     * @ApiParams   (name="region",type=int,required=false,description="区")
     * @ApiParams   (name="area_id",type=int,required=true,description="第三级别的id")
     * @ApiParams   (name="address_id",type=int,required=false,description="收货地址id")
     * @ApiParams   (name="action",type=string,required=false,description="修改时候传edit,设置默认时候不用传")
     */
    public function action_address(){
        
        $params = input();
        $params['user_id'] = $this->user_id;

        if($params['address_id']){//修改地址
            $Address = new Address();
            $address_id = $params['address_id'];
            unset($params['address_id']);
            unset($params['action']);
            if(array_key_exists('isdefault', $params))
            {    
                if($params['isdefault']==1){//修改默认地址
                    Db::name('address')->where(['user_id'=>$this->user_id])->update(['isdefault'=>0]);

                    $Address->allowField(true)->where('id',$address_id)->update($params);
                }
            }else{//修改收获地址信息
                    $Address->allowField(true)->where('id',$address_id)->update($params);
                }
        }else{//添加地址
            $exist_data = Db::name('address')->where(['user_id'=>$this->user_id])->find();
          if(empty($exist_data))$params['isdefault']=1;       
          $Address = new Address($params);
          $Address->allowField(true)->save();
        }


        $this->success('操作成功');

    }

    /**
     * 轮播图
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/banner)
     * @ApiReturn   ({
        
        })
     */
      public function banner(){
        $return = Db::name('banner')->order('weigh desc')->select();
        $this->success('轮播图',$return);
      }


    /**
     * 首页的商品列表
     * @ApiSummary  ()
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/index_list)
     * @ApiReturn   ({
        })
    
     */
    public function index_list(){
        
        $category = Db::name('category')->order('weigh desc')->select();
        foreach ($category as $key => $value) {
            $return[$value['name']] = Db::name('litestore_goods')->where(['pid'=>$value['id']])->field('cover,goods_id,goods_name')->order('weigh desc')->select();
        }
        $this->success('首页的商品列表',$return);
    }

     

     /**
     * 搜索商品
     * @param string $goods_name 
     */
    public function search(){
        $goods_name = input('goods_name');

        $data = Db::name('litestore_goods')->where('goods_name','LIKE',"%$goods_name%")->field('cover,goods_id,goods_name')->order('weigh desc')->select();

        $this->success('搜索商品',$data);
    }

     /**
     * 商品详情页面
     * @ApiSummary  (spec_type 20:多规格/10:单规格。)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_detail)
     * @ApiParams   (name="goods_id",type=int,required=true,description="商品id")
     * @ApiReturn ({
       
     })
     */
    public function goods_detail(){
        $goods_id = $this->request->request('goods_id');
        if(empty($goods_id))$this->error('缺少商品id');
        
        $detail = $this->LitestoreGoods->get($goods_id, ['specRel', 'spec', 'spec_rel.spec']);
        if (!$detail)$this->error('很抱歉，商品信息不存在');
        // 规格信息
        $specData= $this->LitestoreGoods->getApiManySpecData($detail['spec_rel'], $detail['spec']);

        $detail = $detail->toArray();
        if($detail['spec_type']=='20'){
          unset($detail['spec_rel']);
          // unset($detail['spec']);
       
          $data = array_merge($detail,$specData);
        }else{

          $data = $detail;
        }
        
        $this->success('商品详情页面',$data);

    }

    
  

}
